<?php
declare (strict_types = 1);

namespace app\home\controller;

use app\home\business\Code;
use app\home\business\User;
use app\home\validate\Register;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Session;
use think\facade\Validate;
use think\facade\View;
use think\Request;

class Login
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function login()
    {
        // 临时关闭模版布局
        View::config(['layout_on' => false]);

        return View::fetch();
    }

    /**
     * 注册页面
     * @param Request $request
     * @return string
     */
    public function register(Request $request){
        if (\request()->isPost()) {
            try {
                # 注册
                $params = $request->all();
                # 验证参数
                \validate(Register::class)->check($params);
                # 添加入库
                User::register($params);
                # 跳转登录页面、登录
                return  redirect('login');
            }catch (ValidateException $exception) {
                return  $exception->getError();
            }catch (Exception $exception) {
                return  $exception->getMessage();
            }

        } else {
            // 临时关闭模版布局
            View::config(['layout_on' => false]);

            return View::fetch();
        }

    }

    /**
     * 发送验证码
     */
    public function sendCode(){
        $return = [
            'code' => 200,
            'msg' => 'ok',
            'data' => []
        ];
        try {
            $mobile = \request()->post('mobile');
            # 验证手机号
            $validate = Validate::rule(['mobile' => 'require|mobile']);
            $check = $validate->check(['mobile' => $mobile]);
            if (!$check) {
                throw new \Exception($validate->getError(),2001);
            }
            # 调用业务逻辑层
            $data = Code::sendMessage($mobile);
            $return['data'] = $data;
            return  json($return);

        }catch (\Exception $exception) {
            $return['code'] = $exception->getCode();
            $return['msg'] = $exception->getMessage();
            return json($return);
        }
    }

    /**
     * @param Request $request
     * @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|string|\think\response\Redirect
     * @throws Exception
     */
    public function doLogin(Request $request){
        try {
            $mobile = $request->post('phone');
            $password = $request->post('password');
            # 验证手机、密码
            $validate = Validate::rule([
                'mobile' => 'require|mobile',
                'password' => 'require|length:6,12',
            ]);
            $check = $validate->check(['mobile' => $mobile,'password' => $password]);
            if (!$check) {
                throw new ValidateException($validate->getError());
            }
            User::doLogin($mobile,$password);

            # 登录成功之后、跳转到首页
            return redirect('/');

        }catch (ValidateException $exception){
            return  $exception->getError();
        }
    }

    /**
     * 退出功能
     */
    public function logout(){
        # 清空session
//        Session::delete('user_info');
        Session::clear();
        # 跳转首页
        return redirect('/');
    }

    /**
     * 回调地址
     */
    public function qqcallback(){
        require_once('./plugins/qq/API/qqConnectAPI.php');
        $qc = new \QC();
        # 获取access_token
        $access_token = $qc->qq_callback();
        # 获取openid
        $openid = $qc->get_openid();
        $qc = new \QC($access_token,$openid);
        # 获取到用户信息
        $user_info = $qc->get_user_info();
        # 添加入库
        User::createUser($user_info,$openid);
        # 跳转到首页
        return redirect('/');
    }


}
